<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../../component/layui/css/layui.css">
    <link rel="stylesheet" href="../../admin/css/pearTable.css">
    <style>
        body{
            margin: 10px;
            background-color:whitesmoke;
        }
    </style>
</head>
<body>
<div class="layui-card" style="max-height: 100%;">
    <div class="layui-card-body">
        <table class="layui-hide" id="demo" lay-filter="test"></table>

        <script type="text/html" id="barDemo">
            <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="publish">发布</a>
            <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="design">流程设计</a>
            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        </script>
    </div>
</div>
<script src="../../component/layui/layui.js"></script>
<script>
    layui.use(['table','layer','jquery'], function(){
        var layer = layui.layer //弹层
            ,table = layui.table,
              $ = layui.jquery;

        table.render({
            elem: '#demo'
            ,method:'post'
            ,url: '/process/model/list' //数据接口
            ,title: '流程定义'
            ,skin:'line'
            ,page: true //开启分页
            ,toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            ,totalRow: true //开启合计行
            ,cols: [
                [ //表头
                    {type: 'checkbox', fixed: 'left'}
                    ,{field: 'id', title: '流程编号', width:80, sort: true, fixed: 'left'}
                    ,{field: 'name', title: '流程名称'}
                    ,{field: 'key', title: '流程标识', sort: true}
                    ,{field: 'version', title: '流程版本',  sort: true}
                    ,{fixed: 'right',title:'操作',width: 250, align:'center', toolbar: '#barDemo'}
                ]
            ]
        });

        //监听头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id)
                ,data = checkStatus.data; //获取选中的数据
            switch(obj.event){
                case 'add':
                    location.href="/process/model/create?name=www&key=555";
                    break;
                case 'update':
                    if(data.length === 0){
                        layer.msg('请选择一行');
                    } else if(data.length > 1){
                        layer.msg('只能同时编辑一个');
                    } else {
                        layer.alert('编辑 [id]：'+ checkStatus.data[0].id);
                    }
                    break;
                case 'delete':
                    if(data.length === 0){
                        layer.msg('请选择一行');
                    } else {
                        layer.msg('删除');
                    }
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                ,layEvent = obj.event; //获得 lay-event 对应的值
            if(layEvent === 'detail'){
                layer.msg('查看操作');
            } else if(layEvent === 'del'){
                layer.confirm('真的删除行么', function(index){
                    $.ajax({
                        url:'/act/model/deleteById',
                        data:{modelId:obj.data.id},
                        dataType:'json',
                        type:'post',
                        success:function(data){
                            if(data.success){

                                layer.msg(data.msg,{icon:1,time:1000},function(){
                                    obj.del(); //删除对应行（tr）的DOM结构
                                    layer.close(index);
                                    //向服务端发送删除指令
                                })

                            }else{

                                layer.msg(data.msg,{icon:2,time:1000});

                            }
                        }

                    })


                });
            } else if(layEvent === 'edit'){
                layer.msg('编辑操作');
            } else if(layEvent === 'design'){

                location.href = "/process/model/editor?modelId="+obj.data.id;
            } else if(layEvent === 'publish'){

                $.ajax({
                    url:'/act/model/publish',
                    data:{modelId:obj.data.id},
                    dataType:'json',
                    type:'post',
                    success:function(data){
                        if(data.success){
                            layer.msg(data.msg,{icon:1,time:1000});
                        }else{
                            layer.msg(data.msg,{icon:2,time:1000});
                        }
                    }
                })
            }
        });



    });
</script>
</body>
</html>